package top.lixunda.ecommerce.server.good.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import top.lixunda.commerce.common.core.entity.MessageObject;
import top.lixunda.common.utils.ObjectValidators;
import top.lixunda.common.validate.XumValid;
import top.lixunda.ecommerce.server.business.BaseController;
import top.lixunda.ecommerce.server.good.api.controller.IPageIndexController;
import top.lixunda.ecommerce.server.good.api.entity.dto.PageIndexCarouselChangeDTO;
import top.lixunda.ecommerce.server.good.api.entity.dto.PageIndexCarouselInsertDTO;
import top.lixunda.ecommerce.server.good.api.entity.dto.PageIndexFeaturedChangeDTO;
import top.lixunda.ecommerce.server.good.api.entity.dto.PageIndexFeaturedInsertDTO;
import top.lixunda.ecommerce.server.good.api.entity.vo.PageCarouselVO;
import top.lixunda.ecommerce.server.good.api.entity.vo.PageFeaturedVO;
import top.lixunda.ecommerce.server.good.service.IPageIndexService;

import java.util.Collections;
import java.util.List;

/**
 * 前端页面index页面数据
 *
 * @author lixunda
 * @version time:2020/3/18 11:29
 */
@Slf4j
@RestController
public class PageIndexController extends BaseController
        implements IPageIndexController {

    private final IPageIndexService pageIndexService;

    @Autowired
    public PageIndexController(IPageIndexService pageIndexService) {
        this.pageIndexService = pageIndexService;
    }

    /**
     * 获取首页走马灯数据
     *
     * @return PageCarouselVO列表
     */
    @Override
    public MessageObject<List<PageCarouselVO>> queryCarouselVO() {
        List<PageCarouselVO> list = pageIndexService.queryCarouselVO();
        return getSuccessMessageObject("获取数据成功", list);
    }

    /**
     * 添加保存一条首页的Carousel数据
     *
     * @param pageIndexCarouselInsertDTO 保存数据
     * @return 是否保存成功
     */
    @Override
    public MessageObject<Boolean> insertIndexCarousel(@XumValid PageIndexCarouselInsertDTO pageIndexCarouselInsertDTO) {
        boolean success = pageIndexService.insertIndexCarousel(pageIndexCarouselInsertDTO);
        return success ? getSuccessMessageObject("保存数据成功", true) :
                getErrorMessageObject("保存数据失败", false);
    }

    /**
     * 添加保存一条首页的Carousel数据
     *
     * @param list 保存数据列表
     * @return 是否保存成功
     */
    @Override
    public MessageObject<Boolean> insertIndexCarouselBatch(List<PageIndexCarouselInsertDTO> list) {
        boolean success = pageIndexService.insertIndexCarouselBatch(list);
        return success ? getSuccessMessageObject("保存数据成功", true) :
                getErrorMessageObject("保存数据失败", false);
    }

    /**
     * 修改首页轮播图数据
     *
     * @param pageIndexCarouselChangeDTO 轮播图数据修改DTO
     * @return 是否修改成功
     */
    @Override
    public MessageObject<Boolean> changeIndexCarousel(@XumValid PageIndexCarouselChangeDTO pageIndexCarouselChangeDTO) {
        boolean success = pageIndexService.changeIndexCarousel(pageIndexCarouselChangeDTO);
        return success ? getSuccessMessageObject("修改数据成功", true) :
                getErrorMessageObject("修改数据失败", false);
    }

    /**
     * 根据id移除数据
     *
     * @param ids 轮播图数据编号列表
     * @return 是否移除成功
     */
    @Override
    public MessageObject<Boolean> removeIndexCarouselByIds(List<Integer> ids) {
        boolean success = pageIndexService.removeIndexCarouselByIds(ids);
        return success ? getSuccessMessageObject("移除数据成功", true) :
                getErrorMessageObject("移除数据失败", false);
    }

    /**
     * 获取首页Featured数据
     *
     * @return PageFeaturedVO列表
     */
    @Override
    public MessageObject<List<PageFeaturedVO>> queryFeaturedVO() {
        List<PageFeaturedVO> list = pageIndexService.queryFeaturedVO();
        return getSuccessMessageObject("获取数据成功", list);
    }

    /**
     * 添加保存一条首页的Featured数据
     *
     * @param pageIndexFeaturedInsertDTO 保存数据
     * @return 保存后的数据，为空表示失败
     */
    @Override
    public MessageObject<PageFeaturedVO> insertIndexFeatured(PageIndexFeaturedInsertDTO pageIndexFeaturedInsertDTO) {
        PageFeaturedVO vo = pageIndexService.insertIndexFeatured(pageIndexFeaturedInsertDTO);
        return ObjectValidators.isEmpty(vo) ? getErrorMessageObject("添加数据失败", null) :
                getSuccessMessageObject("添加数据成功", vo);
    }

    /**
     * 添加保存多条首页的Featured数据
     *
     * @param list 保存数据列表
     * @return 保存后的数据，为空表示失败
     */
    @Override
    public MessageObject<List<PageFeaturedVO>> insertIndexFeaturedBatch(List<PageIndexFeaturedInsertDTO> list) {
        List<PageFeaturedVO> resultList = pageIndexService.insertIndexFeaturedBatch(list);
        return ObjectValidators.isEmpty(resultList) ? getErrorMessageObject("添加数据失败", Collections.emptyList()) :
                getSuccessMessageObject("添加数据成功", resultList);
    }

    /**
     * 修改首页Featured数据
     *
     * @param pageIndexFeaturedChangeDTO Featured数据修改DTO
     * @return 修改后的数据
     */
    @Override
    public MessageObject<PageFeaturedVO> changeIndexFeatured(PageIndexFeaturedChangeDTO pageIndexFeaturedChangeDTO) {
        PageFeaturedVO vo = pageIndexService.changeIndexFeatured(pageIndexFeaturedChangeDTO);
        return ObjectValidators.isEmpty(vo) ? getErrorMessageObject("修改数据失败", null) :
                getSuccessMessageObject("修改数据成功", vo);
    }

    /**
     * 根据id移除数据
     *
     * @param ids Featured据编号列表
     * @return 是否移除成功
     */
    @Override
    public MessageObject<Boolean> removeIndexFeaturedByIds(List<Integer> ids) {
        boolean success = pageIndexService.removeIndexFeaturedByIds(ids);
        return success ? getSuccessMessageObject("移除数据成功", true) :
                getErrorMessageObject("移除数据失败", false);
    }

}
